package com.dkd.easybook.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dkd.common.annotation.DataScope;
import com.dkd.common.enums.UpdateType;
import com.dkd.common.utils.EbUtils;
import com.dkd.easybook.domain.EbAccount;
import com.dkd.easybook.domain.EbAttachments;
import com.dkd.easybook.domain.EbIcons;
import com.dkd.easybook.domain.EbUserAccountCategory;
import com.dkd.easybook.mapper.EbAccountMapper;
import com.dkd.easybook.service.IEbAccountService;
import com.dkd.easybook.tools.EbTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
/**
 * 记账列表Service业务层处理
 *
 * @author itheima
 * @date 2024-12-13
 */
@Service
public class EbAccountServiceImpl extends ServiceImpl<EbAccountMapper, EbAccount> implements IEbAccountService {
    @Autowired
    private EbAccountMapper ebAccountMapper;
    @Autowired
    private EbAttachmentRelationsServiceImpl ebAttachmentRelationsServiceImpl;
    @Autowired
    private EbAttachmentsServiceImpl ebAttachmentsServiceImpl;
    @Autowired
    private EbUserAccountCategoryServiceImpl ebUserAccountCategoryServiceImpl;
    @Autowired
    private EbIconsServiceImpl ebIconsServiceImpl;
    /**
     * 查询记账列表
     *
     * @param accountId 记账列表主键
     * @return 记账列表
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public EbAccount selectEbAccountByAccountId(Long accountId) {
        EbAccount ebAccount = new EbAccount();
        ebAccount.setAccountId(accountId);
        EbAccount ebAccountResult = ebAccountMapper.selectEbAccountByAccountId(ebAccount);
        List<EbAttachments> listEbAttachmentsExist = EbTools.getAttachmentsByRecordId("eb_account",
                ebAccountResult.getAccountId());
        ebAccountResult.setAttachmentCount(listEbAttachmentsExist.size());
        ebAccountResult.setAttachmentIds(listEbAttachmentsExist.stream()
                .map(EbAttachments::getAttachmentId)
                .collect(Collectors.toList()));
        ebAccountResult.setListAttachments(listEbAttachmentsExist);
        return ebAccountResult;
    }
    /**
     * 查询记账列表列表
     *
     * @param ebAccount 记账列表
     * @return 记账列表
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<EbAccount> selectEbAccountList(EbAccount ebAccount) {
        List<EbAccount> list = ebAccountMapper.selectEbAccountList(ebAccount);
        list.forEach(ebAccountResult -> {
            EbUserAccountCategory ebUserAccountCategory = ebUserAccountCategoryServiceImpl.getById(ebAccountResult.getCategoryId());
            EbIcons ebIcons = ebIconsServiceImpl.getById(ebUserAccountCategory.getIconId());
            if (ebIcons != null) {
                String iconUrl = ebIcons.getUrl();
                if (iconUrl != null) {
                    ebAccountResult.setIconUrl(iconUrl);
                }
            }
        });
        return list;
    }
    /**
     * 新增记账列表
     *
     * @param ebAccount 记账列表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEbAccount(EbAccount ebAccount) {
        //设置用户id
        EbUtils.setUserIdIfNull(ebAccount,
                EbAccount::getUserId,
                EbAccount::setUserId);
        List<Long> attachmentIds = ebAccount.getAttachmentIds();
        EbUtils.updateTableFields(UpdateType.ADD_FIELD,
                ebAccount);
        //先插入记账表
        Integer insertEbAccountResult = ebAccountMapper.insert(ebAccount);
        //插入附件关系表
        EbTools.handleIsertAttachmentsRelations("eb_account",
                ebAccount.getAccountId(),
                attachmentIds);
        return insertEbAccountResult;
    }
    /**
     * 修改记账列表
     *
     * @param ebAccount 记账列表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateEbAccount(EbAccount ebAccount) {
        EbUtils.updateTableFields(UpdateType.UPDATE_FIELD,
                ebAccount);
        Integer updateEbAccountResult = ebAccountMapper.updateById(ebAccount);
        List<Long> attachmentIds = ebAccount.getAttachmentIds();
        //再插入附件关系表
        EbTools.handleIsertAttachmentsRelations("eb_account",
                ebAccount.getAccountId(),
                attachmentIds);
        return updateEbAccountResult;
    }
    /**
     * 批量删除记账列表
     *
     * @param accountIds 需要删除的记账列表主键
     * @return 结果
     */
    @Override
    public int deleteEbAccountByAccountIds(Long[] accountIds) {
        return ebAccountMapper.deleteEbAccountByAccountIds(accountIds);
    }
    /**
     * 删除记账列表信息
     *
     * @param accountId 记账列表主键
     * @return 结果
     */
    @Override
    public int deleteEbAccountByAccountId(Long accountId) {
        return ebAccountMapper.deleteEbAccountByAccountId(accountId);
    }
}
